/*******************************************************************************
 * Copyright 2008 www.softao.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
/**
 * @(#)FocusHelper.java Jul 20, 2008
 *
 * Placeholder for copyright, needs to be replaced before release.
 */
package com.softao.ming.gwt.user.client;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.HasFocus;

/**
 * Helps to focus a widget which has focus.
 */
public final class FocusHelper {
	/**
	 * This is a safer way to ensure that the control will be focused.
	 * 
	 * @author Dop Sun Jul 15, 2008
	 */
	private static class FocusWidgetCommand implements Command {
		private final HasFocus mWidget;

		/**
		 * Initializes a new instance of
		 * <code>ValidationUnit.FocusWidgetCommand</code>.
		 * 
		 * @param widget
		 *            the widget to focus
		 */
		public FocusWidgetCommand(HasFocus widget) {
			mWidget = widget;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see com.google.gwt.user.client.Command#execute()
		 */
		public void execute() {
			this.mWidget.setFocus(true);
		}

	}

	/**
	 * Set focus to the <code>widget</code> after all current event has been
	 * completed.
	 * 
	 * @param widget
	 *            the widget to be focused
	 */
	public static final void setFocus(HasFocus widget) {
		DeferredCommand.addCommand(new FocusWidgetCommand(widget));
	}
}
